package com.ruoyi.report_kf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_kf.domain.ReportKfTlsxzsjlbBaDetail;
import com.ruoyi.report_kf.domain.vo.ReportKfTlsxzsjlbBaDetailVo;
import com.ruoyi.report_kf.mapper.ReportKfTlsxzsjlbBaDetailMapper;
import com.ruoyi.report_tg.domain.TdczbhslBaDetail;
import com.ruoyi.report_tg.domain.vo.TdczbhslBaDetailVo;
import com.ruoyi.syrw.annotation.DealHeh;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_kf.domain.bo.ReportKfTlsxzsjlbBsInfoBo;
import com.ruoyi.report_kf.domain.vo.ReportKfTlsxzsjlbBsInfoVo;
import com.ruoyi.report_kf.domain.ReportKfTlsxzsjlbBsInfo;
import com.ruoyi.report_kf.mapper.ReportKfTlsxzsjlbBsInfoMapper;
import com.ruoyi.report_kf.service.IReportKfTlsxzsjlbBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 填料塑性指数试验检测记录主Service业务层处理
 *
 * @author zyb
 * @date 2024-11-18
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class ReportKfTlsxzsjlbBsInfoServiceImpl implements IReportKfTlsxzsjlbBsInfoService {

    private final ReportKfTlsxzsjlbBsInfoMapper baseMapper;
    private final ReportKfTlsxzsjlbBaDetailMapper detailMapper;

    /**
     * 查询填料塑性指数试验检测记录主
     */
    @Override
    @FIllBasicInfo
    public ReportKfTlsxzsjlbBsInfoVo queryById(Long excelId){
        ReportKfTlsxzsjlbBsInfoVo reportKfTlsxzsjlbBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<ReportKfTlsxzsjlbBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<ReportKfTlsxzsjlbBaDetail>()
            .eq(ReportKfTlsxzsjlbBaDetail::getExcelId, excelId)
            .orderByAsc(ReportKfTlsxzsjlbBaDetail::getBaRowNumber));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            reportKfTlsxzsjlbBsInfoVo.setBaDetailVos1(baDetailVos.subList(0, 6));
            reportKfTlsxzsjlbBsInfoVo.setBaDetailVos2(baDetailVos.subList(6, 8));
        }
        return reportKfTlsxzsjlbBsInfoVo;
    }

    /**
     * 查询填料塑性指数试验检测记录主列表
     */
    @Override
    public TableDataInfo<ReportKfTlsxzsjlbBsInfoVo> queryPageList(ReportKfTlsxzsjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportKfTlsxzsjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportKfTlsxzsjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询填料塑性指数试验检测记录主列表
     */
    @Override
    public List<ReportKfTlsxzsjlbBsInfoVo> queryList(ReportKfTlsxzsjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportKfTlsxzsjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportKfTlsxzsjlbBsInfo> buildQueryWrapper(ReportKfTlsxzsjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportKfTlsxzsjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportKfTlsxzsjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportKfTlsxzsjlbBsInfo::getPage, bo.getPage());
//        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportKfTlsxzsjlbBsInfo::getJilbh, bo.getJilbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportKfTlsxzsjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
//        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportKfTlsxzsjlbBsInfo::getGongcmc, bo.getGongcmc());
//        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportKfTlsxzsjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportKfTlsxzsjlbBsInfo::getYangpmc, bo.getYangpmc());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportKfTlsxzsjlbBsInfo::getYangpbh, bo.getYangpbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportKfTlsxzsjlbBsInfo::getYangpsl, bo.getYangpsl());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportKfTlsxzsjlbBsInfo::getYangpzt, bo.getYangpzt());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportKfTlsxzsjlbBsInfo::getYangpsj, bo.getYangpsj());
//        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportKfTlsxzsjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
//        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportKfTlsxzsjlbBsInfo::getShiytjwd, bo.getShiytjwd());
//        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportKfTlsxzsjlbBsInfo::getShiytjsd, bo.getShiytjsd());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportKfTlsxzsjlbBsInfo::getJiancyj, bo.getJiancyj());
//        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportKfTlsxzsjlbBsInfo::getPandyj, bo.getPandyj());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportKfTlsxzsjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
//        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportKfTlsxzsjlbBsInfo::getPandyjIds, bo.getPandyjIds());
//        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportKfTlsxzsjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportKfTlsxzsjlbBsInfo::getShebIds, bo.getShebIds());
//        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportKfTlsxzsjlbBsInfo::getFujsm, bo.getFujsm());
//        lqw.eq(bo.getJiancjg() != null, ReportKfTlsxzsjlbBsInfo::getJiancjg, bo.getJiancjg());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpsfff()), ReportKfTlsxzsjlbBsInfo::getYangpsfff, bo.getYangpsfff());
//        lqw.eq(StringUtils.isNotBlank(bo.getShiycs1()), ReportKfTlsxzsjlbBsInfo::getShiycs1, bo.getShiycs1());
//        lqw.eq(StringUtils.isNotBlank(bo.getShiycs2()), ReportKfTlsxzsjlbBsInfo::getShiycs2, bo.getShiycs2());
//        lqw.eq(StringUtils.isNotBlank(bo.getYex1()), ReportKfTlsxzsjlbBsInfo::getYex1, bo.getYex1());
//        lqw.eq(StringUtils.isNotBlank(bo.getYex2()), ReportKfTlsxzsjlbBsInfo::getYex2, bo.getYex2());
//        lqw.eq(StringUtils.isNotBlank(bo.getYexcdz()), ReportKfTlsxzsjlbBsInfo::getYexcdz, bo.getYexcdz());
//        lqw.eq(StringUtils.isNotBlank(bo.getPingjsx()), ReportKfTlsxzsjlbBsInfo::getPingjsx, bo.getPingjsx());
//        lqw.eq(StringUtils.isNotBlank(bo.getSuxzs()), ReportKfTlsxzsjlbBsInfo::getSuxzs, bo.getSuxzs());
        return lqw;
    }

    /**
     * 新增填料塑性指数试验检测记录主
     */
    @Override
    public Boolean insertByBo(ReportKfTlsxzsjlbBsInfoBo bo) {
        ReportKfTlsxzsjlbBsInfo add = BeanUtil.toBean(bo, ReportKfTlsxzsjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细
            ArrayList<ReportKfTlsxzsjlbBaDetail> details = new ArrayList<>(8);
            for (long i = 1; i <= 8L; i++) {
                details.add(new ReportKfTlsxzsjlbBaDetail(add.getExcelId(), i));
            }
            detailMapper.insertBatch(details, 8);
        }
        return flag;
    }

    /**
     * 修改填料塑性指数试验检测记录主
     */
    @Override
    @DealHeh
    public Boolean updateByBo(ReportKfTlsxzsjlbBsInfoBo bo) {
        ReportKfTlsxzsjlbBsInfo update = BeanUtil.toBean(bo, ReportKfTlsxzsjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos1(), ReportKfTlsxzsjlbBaDetail.class));
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos2(), ReportKfTlsxzsjlbBaDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportKfTlsxzsjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除填料塑性指数试验检测记录主
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
